package com.chinamobile.maps.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.chinamobile.maps.dao.IChannelnetworksDAO;
import com.chinamobile.maps.dao.IRegionsDAO;
import com.chinamobile.maps.orm.Channelnetworks;
import com.chinamobile.maps.orm.Regions;
import com.chinamobile.maps.util.GenerateUuid;
import com.chinamobile.maps.util.ImportUtils;
import com.chinamobile.maps.util.LoggerUtil;
import com.chinamobile.maps.util.Struts2Util;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: UpDownloadAction
 * @Description: TODO
 * @author: frankwong
 * @date: 2012-10-28 下午4:16:46
 * 
 */
public class UpDownloadAction extends ActionSupport {

	public IChannelnetworksDAO channelnetworksDAO = null;
	public IRegionsDAO regionDao = null;
	/**
	 * @return the regionDao
	 */
	public IRegionsDAO getRegionDao() {
		return regionDao;
	}

	/**
	 * @param regionDao the regionDao to set
	 */
	public void setRegionDao(IRegionsDAO regionDao) {
		this.regionDao = regionDao;
	}

	/**
	 * @return the channelnetworksDAO
	 */
	public IChannelnetworksDAO getChannelnetworksDAO() {
		return channelnetworksDAO;
	}

	/**
	 * @param channelnetworksDAO
	 *            the channelnetworksDAO to set
	 */
	public void setChannelnetworksDAO(IChannelnetworksDAO channelnetworksDAO) {
		this.channelnetworksDAO = channelnetworksDAO;
	}

	private static final long serialVersionUID = 1L;
	private File file;// 对应jsp中file标签
	private String fileFileName;//
	private String fileContentType;//
	private String fileName;// 获得jsp中pram参数

	private File pic;// 对应jsp中file标签
	private String picFileName;//
	private String picContentType;//
	
	@SuppressWarnings("deprecation")
	// 文件上传
	public void uploadFiile() throws Exception {
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");// 上传路径
		InputStream inputStream = null;
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = null;
		out = (PrintWriter) response.getWriter();
		try{
			File destFile = new File(fileName);
			List<Channelnetworks> list = ImportUtils
					.importChannelNetworksFromExcel(fileName);
			int count = list.size();
			for (int i = 0; i < count; i++) {
				this.channelnetworksDAO.addChannelnetworks(list.get(i));
				operationByChannel(1,list.get(i).getNetworkType(),list.get(i).getRegionName(),list.get(i).getMicroRegion());
			}
			LoggerUtil.LogInfo("上传渠道网点数据共 "+count+" 条");
			out.print("{\"result\":true}");
			out.flush();
			out.close();
		}catch(Exception e){
			out.print("{\"result\":false}");
			out.flush();
			out.close();
		}
	}

	public void uploadPic() throws Exception{
		String root = ServletActionContext.getServletContext().getRealPath(
				"/upload");// 上传路径
		InputStream inputStream = null;
		HttpServletResponse response = Struts2Util.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		out = (PrintWriter) response.getWriter();
		try{
			String[] strArray = picContentType.split("/");
			if(strArray[1].equals("pjpeg")){
				strArray[1] = "jpg";
			}
			String picUrl = GenerateUuid.generateUuid()+"."+strArray[1];
			File target = new File("c:/MobileMaps/pic", picUrl);
            FileUtils.copyFile(pic, target);    
			out.print("{\"name\":'"+picUrl+"'}");
			out.flush();
			out.close();
		}catch(Exception e){
			out.print("{\"result\":false}");
			out.flush();
			out.close();
		}
	}
	// 文件下载
	public InputStream getDownloadFile() throws FileNotFoundException,
			UnsupportedEncodingException {

		// 如果下载文件名为中文，进行字符编码转换
		ServletActionContext.getResponse().setHeader(
				"Content-Disposition",
				"attachment;fileName="
						+ java.net.URLEncoder.encode(fileName, "UTF-8"));
		InputStream inputStream = new FileInputStream("F:/" // 使用绝对路径
															// ，从该路径下载“测试.txt"文件
				+ this.getFileName());
		return inputStream;
	}

	public void operationByChannel(int type,String networkType,String regionName,String microRegionName){
		try{
			Regions mainRegion = this.regionDao.getRegionsById(1);
			Regions region = this.regionDao.getRegionsByName(regionName);
			Regions microRegion = this.regionDao.getRegionsByName(microRegionName);
			
			if(networkType.equals("移动主厅")){
				region.setMainHallCount(region.getMainHallCount()+type);
				mainRegion.setMainHallCount(mainRegion.getMainHallCount()+type);
				microRegion.setMainHallCount(microRegion.getMainHallCount()+type);
			}else if(networkType.equals("移动合作厅")){
				region.setCooperativeHallCount(region.getCooperativeHallCount()+type);
				mainRegion.setCooperativeHallCount(mainRegion.getCooperativeHallCount()+type);
				microRegion.setCooperativeHallCount(microRegion.getCooperativeHallCount()+type);
			}else if(networkType.equals("特约代理店")){
				region.setSpecialAgentCount(region.getSpecialAgentCount()+type);
				mainRegion.setSpecialAgentCount(mainRegion.getSpecialAgentCount()+type);
				microRegion.setSpecialAgentCount(microRegion.getSpecialAgentCount()+type);
			}else if(networkType.equals("授权销售点")){
				region.setAuthorizedPointCount(region.getAuthorizedPointCount()+type);
				mainRegion.setAuthorizedPointCount(mainRegion.getAuthorizedPointCount()+type);
				microRegion.setAuthorizedPointCount(microRegion.getAuthorizedPointCount()+type);
			}else if(networkType.equals("村级便民点")){
				region.setVillagePointCount(region.getVillagePointCount()+type);
				mainRegion.setVillagePointCount(mainRegion.getVillagePointCount()+type);
				microRegion.setVillagePointCount(microRegion.getVillagePointCount()+type);
			}else if(networkType.equals("联通营业厅")){
				region.setUnicomHallCount(region.getUnicomHallCount()+type);
				mainRegion.setUnicomHallCount(mainRegion.getUnicomHallCount()+type);
				microRegion.setUnicomHallCount(microRegion.getUnicomHallCount()+type);
			}else if(networkType.equals("电信营业厅")){
				region.setTelecomHallCount(region.getTelecomHallCount()+type);
				mainRegion.setTelecomHallCount(mainRegion.getTelecomHallCount()+type);
				microRegion.setTelecomHallCount(microRegion.getTelecomHallCount()+type);
			}else if(networkType.equals("共享渠道")){
				region.setShareChannelCount(region.getShareChannelCount()+type);
				mainRegion.setShareChannelCount(mainRegion.getShareChannelCount()+type);
				microRegion.setShareChannelCount(microRegion.getShareChannelCount()+type);
			}else{
			}
			this.regionDao.updateRegions(mainRegion);
			this.regionDao.updateRegions(region);
			this.regionDao.updateRegions(microRegion);
		}catch(Exception e){
			
		}
		
	}
	
	// 下载
	public String downloadFile() throws Exception {
		return SUCCESS;
	}

	public String getFileName() throws UnsupportedEncodingException {
		return fileName;
	}

	public void setFileName(String fileName)
			throws UnsupportedEncodingException {
		this.fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");
	}

	/**
	 * @return the fileFileName
	 */
	public String getFileFileName() {
		return fileFileName;
	}

	/**
	 * @param fileFileName
	 *            the fileFileName to set
	 */
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	/**
	 * @return the fileContentType
	 */
	public String getFileContentType() {
		return fileContentType;
	}

	/**
	 * @param fileContentType
	 *            the fileContentType to set
	 */
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	/**
	 * @return the pic
	 */
	public File getPic() {
		return pic;
	}

	/**
	 * @param pic the pic to set
	 */
	public void setPic(File pic) {
		this.pic = pic;
	}

	/**
	 * @return the picFileName
	 */
	public String getPicFileName() {
		return picFileName;
	}

	/**
	 * @param picFileName the picFileName to set
	 */
	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}

	/**
	 * @return the picContentType
	 */
	public String getPicContentType() {
		return picContentType;
	}

	/**
	 * @param picContentType the picContentType to set
	 */
	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}
}
